import { Component, OnInit } from '@angular/core';
import { AxiosService } from '../../axios.service';

@Component({
  selector: 'app-permissions-cncjiagong',
  templateUrl: './permissions-cncjiagong.component.html',
  styleUrls: ['./permissions-cncjiagong.component.scss']
})
export class PermissionsCNCjiagongComponent implements OnInit {
  name; // 选择的机台号
  diameter_max; // 直径
  length_max; // 长
  width_max; // 宽
  stuff_type; // 材质类型
  stuff_range; // 材质范围
  id;
  isVisible;
  listOfData = [];
  widthConfig = ['100px', '100px', '100px', '100px', '150px', '350px', '100px'];
  scrollConfig = { x: '1000px', y: '220px' };

  constructor(public axios: AxiosService) { }

  ngOnInit(): void {
    this.queryCNC();
  }

  // 查看cnc设备
  queryCNC() {
    this.axios.axiosPost('QueryCncInfo')
      .then((data) => {
        if (data) {
          console.log(data);
          let a = data['order'];
          a.sort((a,b) =>{
            return a.name > b.name ? 1 : -1;
          })
          this.listOfData = a;
          console.log(this.listOfData);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }

  // 删除设备
  handleOk() {
    this.axios.axiosPost('DeleteCncInfo', { id: this.id })
      .then((data) => {
        if (data['result'] === 'ok') {
          this.queryCNC();
          console.log(data);
        }
      })
      .catch((err) => {
        console.log(err);
      });
    this.isVisible = false;
  }
  showModal(data): void {
    this.id = data.id;
    console.log(this.id);
    this.isVisible = true;
  }
  handleCancel(): void {
    this.isVisible = false;
  }

  // 修改
  edit(data) {
    this.name = data.name;
    this.diameter_max = data.diameter_max;
    this.length_max = data.length_max;
    this.width_max = data.width_max;
    this.stuff_type = data.stuff_type;
    this.stuff_range = data.stuff_range;
    this.id = data.id;
  }

  // 清除input中的信息
  clear() {
    this.name = '';
    this.diameter_max = '';
    this.length_max = '';
    this.width_max = '';
    this.stuff_type = '';
    this.stuff_range = '';
  }

  // 保存修改
  save() {
    this.axios.axiosPost('UpdateCncInfo', { id: this.id, diameter_max: this.diameter_max, length_max: this.length_max, width_max:this.width_max, stuff_type:this.stuff_type, stuff_range:this.stuff_range})
      .then((data) => {
        console.log(data);
        if (data['result'] === 'ok') {
          this.queryCNC();
          console.log(data);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  }
}
